package com.educal.Controller;

import com.educal.Bean.Teacher;
import com.educal.Repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by miyang on 2017/12/17.
 */
@RestController
public class Admin_TeacherController {
    @Autowired
    private TeacherRepository teacherRepository;

    /*
    * 增加老师
    * */
    @PostMapping(value = "/teachers")
    public Map<String, Object> addTeacher(@RequestParam("tId") String tId,
                          /*@RequestParam("tPassword") String tPssword,*/
                          @RequestParam("tName") String tName,
                          @RequestParam("tCollege") String tCollege,
                          @RequestParam("phone") String phone,
                          @RequestParam("position") String position){
        Teacher teacher = new Teacher();
        Map<String, Object> result = new HashMap<String, Object>();
        teacher.setTid(tId);
        teacher.setTpassword("123456");
        teacher.setTname(tName);
        teacher.setTcollege(tCollege);
        teacher.setPhone(phone);
        teacher.setPosition(position);
        //System.out.print(stu.getStuid()+ stu.getStuname());
        teacher = teacherRepository.save(teacher);
        if(teacher!=null) {
            result.put("code", 200);
            result.put("stu", teacher);
        }else{
            result.put("code", 400);
        }
        //System.out.print(stu.getStuid()+ stu.getStuname());
        return result;
    }

    /*
    * 查询全部学生信息*/
    @GetMapping(value= "/teachers")
    public Map<String, Object> findAll() {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Teacher> teacherList = teacherRepository.findAll();
        if(teacherList!=null&&!teacherList.isEmpty()){
            result.put("code",200);
            result.put("teacherList",teacherList);
        }else{
            result.put("code",400);
        }
        return result;
    }

    /*
    * 用id查询*/
    @GetMapping(value = "/teachers/{tId}")
    public Map<String, Object> findByTeacherId(@PathVariable("tId") String tId) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code",400);
        Teacher teacher = new Teacher();
        List<Teacher> list = teacherRepository.findAll();
        for(int i=0;i<list.size();i++){
            String teaId = list.get(i).getTid();
            if(teaId.equals(tId)) {
                teacher.setTid(tId);
                teacher.setTpassword(list.get(i).getTpassword());
                teacher.setTname(list.get(i).getTname());
                teacher.setTcollege(list.get(i).getTcollege());
                teacher.setPhone(list.get(i).getPhone());
                teacher.setPosition(list.get(i).getPosition());
                result.put("code", 200);
                result.put("teacher", teacher);
                break;
            }
        }
        return result;
    }
    /*
    * 用name查询*/
    @GetMapping(value = "/teachers/name/{tName}")
    public Map<String,Object> findByName(@PathVariable("tName") String tName) {
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Teacher> teaList = new ArrayList<>();
        List<Teacher> list = teacherRepository.findAll();
        for(int i=0;i<list.size();i++){
            String teaName = list.get(i).getTname();
            if(teaName.equals(tName)){
                teaList.add(list.get(i));
            }
        }
        if(teaList!=null&&!teaList.isEmpty()){
            result.put("code",200);
            result.put("teaList",teaList);
        }
        // result.put("stuList",stuList);
        return result;
    }
    /*
     * 用College查询*/
    @GetMapping(value = "/teachers/college/{tCollege}")
    public Map<String,Object> findByCollege(@PathVariable("tCollege") String tCollege) {
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Teacher> teaList = new ArrayList<>();
        List<Teacher> list = teacherRepository.findAll();
        for(int i=0;i<list.size();i++){
            String teaCollege = list.get(i).getTcollege();
            if(teaCollege.equals(tCollege)){
                teaList.add(list.get(i));
            }
        }
        if(teaList!=null&&!teaList.isEmpty()){
            result.put("code",200);
            result.put("teaList",teaList);
        }
        // result.put("stuList",stuList);
        return result;
    }
    /*
    * 修改教师信息*/
    @PutMapping(value = "/teachers")
    public Map<String, Object> updateTeacherId(@RequestParam("tId") String tId,
                                   @RequestParam("tPassword") String tPssword,
                                   @RequestParam("tName") String tName,
                                   @RequestParam("tCollege") String tCollege,
                                   @RequestParam("phone") String phone,
                                   @RequestParam("position") String position){
        Teacher teacher = new Teacher();
        Map<String, Object> result = new HashMap<String, Object>();
        teacher.setTid(tId);
        teacher.setTpassword(tPssword);
        teacher.setTname(tName);
        teacher.setTcollege(tCollege);
        teacher.setPhone(phone);
        teacher.setPosition(position);
        //System.out.print(stu.getStuid()+ stu.getStuname());
        teacher = teacherRepository.save(teacher);
        if(teacher!=null) {
            result.put("code", 200);
            result.put("stu", teacher);
        }else{
            result.put("code", 400);
        }
        //System.out.print(stu.getStuid()+ stu.getStuname());
        return result;
    }

    /*
    * 用id删除*/
    @DeleteMapping(value = "/teachers/{tId}")
    public Map<String, Object> deleteTeacher(@PathVariable("tId") String tId){
        Teacher teacher = new Teacher();
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",400);
        List<Teacher> list = teacherRepository.findAll();
        for(int i=0;i<list.size();i++){
            String teaId = list.get(i).getTid();
            if(teaId.equals(tId)){
                teacher.setTid(tId);
                teacher.setTpassword(list.get(i).getTpassword());
                teacher.setTname(list.get(i).getTname());
                teacher.setTcollege(list.get(i).getTcollege());
                teacher.setPhone(list.get(i).getPhone());
                teacher.setPosition(list.get(i).getPosition());
                teacherRepository.delete(teacher);
                result.put("code",200);
                break;
            }
        }
        return result;
    }
}
